package db

import (
	"database-platform/dal"
	"database-platform/logger"
	"database-platform/model"
	"fmt"
	"time"
)

func ListDatabaseRecords(database *model.Database, page, pageSize int64, search string, showCluster bool) ([]*model.Database, uint64, error) {
	var query = dal.GORM
	var records []*model.Database
	var count uint64

	if database.DBType != "" {
		query = query.Where("db_type = ?", database.DBType)
	}
	if database.PhysicalName != "" {
		query = query.Where("physical_name = ?", database.PhysicalName)
	}
	if database.LogicalName != "" {
		query = query.Where("logical_name = ?", database.LogicalName)
	}
	if database.ClusterID != nil {
		query = query.Where("cluster_id = ?", database.ClusterID)
	}
	if database.Env != "" {
		query = query.Where("env = ?", database.Env)
	}
	if database.IsShard {
		query = query.Where("is_shard = ?", database.IsShard)
	}
	if database.AppName != "" {
		query = query.Where("app_name = ?", database.AppName)
	}
	if database.BusinessOne != "" {
		query = query.Where("business_one = ?", database.BusinessOne)
	}
	if database.BusinessTwo != "" {
		query = query.Where("business_two = ?", database.BusinessTwo)
	}
	if database.Status != "" {
		query = query.Where("status = ?", database.Status)
	}

	if search != "" {
		query = query.Where("physical_name like ?", "%"+search+"%").
			Or("logical_name like ?", "%"+search+"%")
	}

	if page > 0 && pageSize > 0 {
		offset := (page - 1) * pageSize
		query = query.Offset(offset).Limit(pageSize)
	}

	if showCluster {
		query = query.Preload("Cluster")
	}

	err := query.Find(&records).Count(&count).Error
	if err != nil {
		msg := fmt.Sprintf("【DB.LOG】 db.database.ListDatabaseRecords sql execute err, err message is %s, database: %v", err, database)
		logger.Logger.Error(msg)
		return nil, 0, err
	}
	return records, count, nil
}

func GetDatabaseRecordById(id int) (record *model.Database, err error) {
	record = &model.Database{}
	tx := dal.GORM.Where("id = ?", id).First(&record)
	if tx.Error != nil {
		msg := fmt.Sprintf("【DB.LOG】 db.database.GetDatabaseRecordById.sql.execute.err.message.%s.id.%v", tx.Error, id)
		logger.Logger.Error(msg)
		return nil, tx.Error
	}

	return record, nil
}

// InsertDatabaseRecord 插入数据库记录
func InsertDatabaseRecord(database *model.Database) error {
	database.UpdateTime = time.Now()
	database.CreateTime = time.Now()
	err := dal.GORM.Create(database).Error
	if err != nil {
		msg := fmt.Sprintf("【DB.LOG】 db.database.InsertDatabaseRecord sql execute err, err message is %s, database: %v", err, database)
		logger.Logger.Error(msg)
		return err
	}
	return nil
}

// UpdateDatabaseRecord 更新数据库记录
func UpdateDatabaseRecord(id int, database *model.Database) error {
	database.UpdateTime = time.Now()
	database.ID = int64(id)
	err := dal.GORM.Save(database).Error
	if err != nil {
		msg := fmt.Sprintf("【DB.LOG】 db.database.UpdateDatabaseRecord sql execute err, err message is %s, database: %v", err, database)
		logger.Logger.Error(msg)
		return err
	}
	return nil
}

// DeleteDatabaseRecord 删除数据库记录
func DeleteDatabaseRecord(id int) error {
	err := dal.GORM.Delete(&model.Database{}, id).Error
	if err != nil {
		msg := fmt.Sprintf("【DB.LOG】 db.database.DeleteDatabaseRecord sql execute err, err message is %s, id: %d", err, id)
		logger.Logger.Error(msg)
		return err
	}
	return nil
}
